package com.dtx.utils;


import org.beetl.core.ReThrowConsoleErrorHandler;
import org.beetl.sql.core.*;
import org.beetl.sql.core.db.DamengStyle;
import org.beetl.sql.core.db.SqlServer2012Style;
import org.beetl.sql.core.db.SqlServerStyle;
import org.beetl.sql.ext.DebugInterceptor;
import org.beetl.sql.gen.SourceBuilder;
import org.beetl.sql.gen.SourceConfig;
import org.beetl.sql.gen.simple.ConsoleOnlyProject;
import org.beetl.sql.gen.simple.EntitySourceBuilder;
import org.beetl.sql.gen.simple.MDSourceBuilder;
import org.beetl.sql.gen.simple.MapperSourceBuilder;

import java.util.ArrayList;
import java.util.List;


public class CodeBuilding {



    public static void main(String[] args) {
        String url= "jdbc:mysql://127.0.0.1:3306/beetlsql?characterEncoding=UTF-8&allowMultiQueries=true&serverTimezone=UTC";
        String driver = "com.mysql.cj.jdbc.Driver";
        String userName = "root";
        String password = "123456";



        ConnectionSource source = ConnectionSourceHelper.getSimple(driver, url, userName, password);
        //source是唯一必须的参数，其他参数都有默认值
        SQLManagerBuilder builder = new SQLManagerBuilder(source);
        //设置NameConversion，这里数据库命名采用下划线风格，使用UnderlinedNameConversion
        builder.setNc(new UnderlinedNameConversion());
        //设置一个拦截器，输出debug日志，包含了sql语句和执行参数，执行时间
        builder.setInters(new Interceptor[]{new DebugInterceptor()});
        //设置数据库分隔，必须跟数据库一样
        builder.setDbStyle(new SqlServerStyle());


        List<SourceBuilder> sourceBuilder = new ArrayList<>();
        SourceBuilder entityBuilder = new EntitySourceBuilder();
        SourceBuilder mapperBuilder = new MapperSourceBuilder();
        SourceBuilder mdBuilder = new MDSourceBuilder();

        sourceBuilder.add(entityBuilder);
        sourceBuilder.add(mapperBuilder);
        sourceBuilder.add(mdBuilder);
        UnderlinedNameConversion nc = new UnderlinedNameConversion();
        SQLManager sqlManager = SQLManager.newBuilder(source).build();
        //设置驼峰
        sqlManager.setNc(nc);
        SourceConfig config = new SourceConfig(sqlManager,sourceBuilder);
        //如果有错误，抛出异常而不是继续运行1
        EntitySourceBuilder.getGroupTemplate().setErrorHandler(new ReThrowConsoleErrorHandler() );

        ConsoleOnlyProject project = new ConsoleOnlyProject();
        String tableName = "log_result";
        //可以在控制台看到生成的所有代码
        config.gen(tableName,project);

    }
}
